import numpy as np

class DataGenerator:
    def __init__(self, rows, cols):
        self.rows = rows
        self.cols = cols

    def generate_data(self):
        # 生成随机数据
        data = np.random.rand(self.rows, self.cols)
        # 添加一列随机数
        random_column = np.random.rand(self.rows, 1)
        return np.hstack((data, random_column))

    def write_data_to_file(self, filename):
        data = self.generate_data()
        # 将数据写入文件
        with open(filename, 'w') as f:
            for row in data:
                f.write(','.join(map(str, row)) + '\n')

    def read_data_from_file(self, filename):
        with open(filename, 'r') as f:
            for line in f:
                print(line.strip())

if __name__ == "__main__":
    num_rows = 5  # 指定行数
    num_cols = 3  # 指定列数

    generator = DataGenerator(num_rows, num_cols)
    filename = 'train_output/trainNum01.txt'  # 文件名

    # 写入数据到 main.py
    generator.write_data_to_file(filename)

    # 从 main.py 读取数据并打印
    print("从 main.py 中读取的数据:")
    generator.read_data_from_file(filename)